package system

import (
	"MoSkeleton/framework/core/dto/request"
	"MoSkeleton/framework/core/models"
	"MoSkeleton/framework/core/motypes"
	"MoSkeleton/models/system"
	"MoSkeleton/services/base"
)

type BlackTokenService struct {
	base.BaseService
}

func (b *BlackTokenService) ExistsBlackToken(token string, tokenType int) bool {
	blackToken := system.BlackToken{}
	result := base.BaseDb.Where("token_str = ? and token_type=?", token, tokenType).Select("id").First(&blackToken)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

func (b *BlackTokenService) AddBlackToken(claims *request.MoClaims, token string, tokenType int, expiresAt motypes.MoTime) error {
	blackToken := system.BlackToken{
		TokenStr:  token,
		TokenType: &tokenType,
		ExpireAt:  &expiresAt,
		MoUserModel: models.MoUserModel{
			CreatedBy: claims.Username,
			UpdatedBy: claims.Username,
		},
	}
	result := base.BaseDb.Create(&blackToken)
	return result.Error
}
